﻿using CL.Business.BLL;
using CL.Business.Entity;
using CL.Enum.Common.Union;
using CL.Json.Entity;
using CL.Tools.Common;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace CL.UnionWeb
{
    /// <summary>
    /// NoticeOper 的摘要说明
    /// </summary>
    public class NoticeOper : IHttpHandler , IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string Action = context.Request.QueryString["action"].Trim();
            switch (Action)
            {
                case "DelByID":
                    {
                        string id = context.Request.QueryString["id"].Trim();
                        this.DelByID(context,id);
                    }
                    break;
                case "submitinfo":
                    {
                        CaseSubmitInfo(context);
                    }
                    break;
                case "upload":
                    {
                        CaseUploadInfo(context);
                    }
                    break;
            }
        }


        protected void CaseUploadInfo(HttpContext context)
        {
            string uploadType = context.Request["uploadtype"];
            string accountMsg = context.Request["accountMsg"];
            string fileType = context.Request["fileType"];
            if (string.IsNullOrEmpty(uploadType) || string.IsNullOrEmpty(accountMsg) || string.IsNullOrEmpty(fileType))
            {
                context.Response.Write("参数错误!");
                return;
            }

            string serverPath = ConfigurationManager.AppSettings["ImageServerPath"];
            if (string.IsNullOrEmpty(serverPath))
            {
                context.Response.Write("服务器存放图片路径没有设置!");
                return;
            }
            var accountBll = new AccountBLL();
            var uInfo = context.Session[CLKeys.SESSION_ADMIN_INFO] as AccountEntity;

            var userInfo = accountBll.QueryEntity(uInfo.AccountID);
            if (null == userInfo)
            {
                context.Response.Write("获取用户信息失败!");
                return;
            }

            string prePath = "/CL.Union_UserImage/" + accountMsg + "/";
            serverPath += prePath;
            if (!Directory.Exists(serverPath))
                Directory.CreateDirectory(serverPath);

            string filePath = "";
            for (int index = 0; index < context.Request.Files.Count; ++index)
            {
                if ("firstIdentity" == uploadType)//身份证正面上传
                {
                    filePath = userInfo.FirstIdentityImage = uploadType + fileType;
                    userInfo.FirstIdentityImage = prePath + userInfo.FirstIdentityImage;
                }
                else if ("secondIdentity" == uploadType)//身份证反面上传
                {
                    filePath = userInfo.SecondIdentityImage = uploadType + fileType;
                    userInfo.SecondIdentityImage = prePath + userInfo.SecondIdentityImage;
                }
                else if ("BankCard" == uploadType)//银行卡上传
                {
                    filePath = userInfo.BankCardImage = uploadType + index.ToString() + fileType;
                    userInfo.BankCardImage = prePath + userInfo.BankCardImage;
                }
                HttpPostedFile f = context.Request.Files[index];
                f.SaveAs(serverPath + filePath);
            }

            accountBll.UpdateEntity(userInfo);
            context.Session[CLKeys.SESSION_ADMIN_INFO] = userInfo;
            context.Response.Write("ok_" + uploadType);
        }


        protected void CaseSubmitInfo(HttpContext context)
        {
            var accountBll = new AccountBLL();
            var uInfo = context.Session[CLKeys.SESSION_ADMIN_INFO] as AccountEntity;

            var userInfo = accountBll.QueryEntity(uInfo.AccountID);
            if (null == userInfo)
            {
                context.Response.Write("获取用户信息失败!");
                return;
            }

            if(string.IsNullOrEmpty(userInfo.FirstIdentityImage) ||
                string.IsNullOrEmpty(userInfo.SecondIdentityImage) ||
                string.IsNullOrEmpty(userInfo.BankCardImage))
            {
                context.Response.Write("请上传身份证或银行卡后提交!");
                return;
            }

            userInfo.AccountStatus = (int)AccountStatusEnum.certification;

            accountBll.UpdateEntity(userInfo);
            context.Session[CLKeys.SESSION_ADMIN_INFO] = userInfo;
            context.Response.Write("ok_");
        }

        protected void DelByID(HttpContext context,string id)
        {
            NoticeBLL bll = new NoticeBLL();
            bool falg = bll.DelEntity(int.Parse(id));
            JsonResult json;
            if (falg)
            {
                json = new JsonResult() { Code = 1, Msg = "删除成功" };
            }
            else
            {
                json = new JsonResult() { Code = 0, Msg = "删除失败" };
            }

            context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(json));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}